﻿Imports System.Drawing

Public Class AquariumDetail
    Inherits ItemDetailPage

    Protected Property PhotoNormalSize() As New Size() With {.width = 512, .height = 256}
    Protected Property PhotoThumbSize() As New Size() With {.width = 128, .height = 64}
    Protected Property AquariumID() As ULong

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        AquariumID = CULng(Request.QueryString.Get("ID"))
        If Not IsPostBack Then
            If Not AquariumID AndAlso Request.QueryString.Get("Action") = "Add" Then
                If InsertionPermitted Then
                    DetailFormView.DefaultMode = FormViewMode.Insert
                Else
                    StopEditing()
                End If
            ElseIf AquariumID AndAlso AquariumExists(AquariumID) Then
                If Request.QueryString.Get("Action") = "Edit" Then
                    If ModificationPermitted Then
                        DetailFormView.DefaultMode = FormViewMode.Edit
                        UpdateDataSource()
                    Else
                        StopEditing()
                    End If
                Else
                    DetailFormView.DefaultMode = FormViewMode.ReadOnly
                    UpdateDataSource()
                End If
            Else
                Response.Redirect("~/Pages/AquariumList.aspx")
            End If
        End If
    End Sub

    Protected Sub SaveButton_Click(sender As Object, e As EventArgs)
        If ModificationPermitted Then
            'Dim gateway As New SiteUserGateway()
            'Dim firstName As TextBox = DetailFormView.FindControl("FirstNameTextBox")
            'Dim lastName As TextBox = DetailFormView.FindControl("LastNameTextBox")
            'Dim photo As FileUpload = DetailFormView.FindControl("PhotoFileUpload")

            'If photo.HasFile() Then
            '    Dim fileName As String = PhotoUtils.getUniqueFileName(photo.FileName)
            '    PhotoUtils.Save(fileName, photo.PostedFile.InputStream, PhotoNormalSize, PhotoThumbSize)
            '    gateway.Update(UserName, firstName.Text, lastName.Text, fileName)
            'Else
            '    gateway.Update(UserName, firstName.Text, lastName.Text)
            'End If

            StopEditing()
        End If
    End Sub

    Protected Sub CancelEditButton_Click(sender As Object, e As EventArgs)
        StopEditing()
    End Sub

    Protected Sub EditButton_Click(sender As Object, e As EventArgs)
        StartEditing()
    End Sub

    Protected Function AquariumExists(ByVal ID As ULong) As Boolean
        Dim gateway As New AquariumGateway()
        Return gateway.Exists(ID)
    End Function

    Protected Overrides ReadOnly Property UserHasOwnership() As Boolean
        Get
            Dim gateway As New AquariumGateway()
            Return gateway.HasOwnership(AquariumID, User.Identity.Name)
        End Get
    End Property

    Protected Sub StartEditing()
        Dim redirectUrl As String = String.Format("~/Pages/AquariumDetail.aspx?ID={0}&Action=Edit", AquariumID)
        Response.Redirect(redirectUrl)
    End Sub

    Protected Sub StopEditing()
        Dim redirectUrl As String = String.Format("~/Pages/AquariumDetail.aspx?ID={0}", AquariumID)
        Response.Redirect(redirectUrl)
    End Sub

    Protected Function getPhotoPath(ByVal fileName As String) As String
        Return PhotoUtils.getNormalPath(fileName)
    End Function

    Protected Sub UpdateDataSource()
        detailViewDataBind()
        commentViewDataBind()
    End Sub

    Private Sub detailViewDataBind()
        Dim gateway As New AquariumGateway()
        DetailFormView.DataSource = gateway.FindByID(AquariumID)
        DetailFormView.DataBind()
    End Sub

    Private Sub commentViewDataBind()
        Dim gateway As New CommentGateway()
        NestedDetailListView.DataSource = gateway.FindByID(AquariumID)
        NestedDetailListView.DataBind()
    End Sub
End Class